package com.wwg.threads.itcast.demo;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class MovieTicketCellTask implements Runnable {

    private static final Logger LOGGER = LoggerFactory.getLogger(MovieTicketCellTask.class);

    private int ticketNum;
    private String ticketType;

    public int getTicketNum() {
        return ticketNum;
    }

    public void setTicketNum(int ticketNum) {
        this.ticketNum = ticketNum;
    }

    public String getTicketType() {
        return ticketType;
    }

    public void setTicketType(String ticketType) {
        this.ticketType = ticketType;
    }

    public MovieTicketCellTask(int ticketNum, String ticketType) {
        this.ticketNum = ticketNum;
        this.ticketType = ticketType;
        LOGGER.info("本次活动有{}张{}票！",ticketNum,ticketType);
    }

    @Override
    public void run() {

        while (true) {
            if (saveCellTicket()) break;
        }
    }

    private synchronized boolean saveCellTicket() {
            if (ticketNum -- < 1) {
                return true;
            }
            LOGGER.info("{} 卖出一张 {} 票，剩余票数：{}" ,Thread.currentThread().getName() ,ticketType,ticketNum);
            return false;
    }
}
